package ACEbrickBreaker;

/**
 * <p>Title: ReadXMLscoresFile</p>
 * <p>Description: ReadXMLscoresFile
 *	This class reads in data from an XML file and displays it in a message box for the user.
 * * @author Caitlin Finn
 * @email caitlin.finn@my.uwrf.edu
 * @author Erin McNeil
 * @email erin.mcneil@my.uwrf.edu
 * @author Andrew Juranek
 * @email andrew.juranek@my.uwrf.edu
 * @date December 10th 2012
 */
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXMLscoresFile extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public ArrayList<String> scoresArray;

	// public static void main(String argv[]) {
	/**
	 * When the object is instantiated the XML file is read
	 */
	public ReadXMLscoresFile() {
		try {

			// reads current high scores from xml file
			File fXmlFile = new File("highScoreCurrent.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();

			// get the elements of the XML file and display them in High Scores
			// window
			NodeList nList = doc.getElementsByTagName("Player");
			String s = "High Scores! \n";
			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					s = s + "\n-------------------------------------------";
					s = s + "\nName : " + getTagValue("name", eElement);
					s = s + "\nScore : " + getTagValue("score", eElement);
					s = s + "\nTime : " + getTagValue("time", eElement);

				}
			}
			JScrollPane scrollPane = new JScrollPane(new JLabel(s));
			scrollPane.setPreferredSize(new Dimension(500, 500));
			Object message = scrollPane;

			// display the high scores
			JTextArea textArea = new JTextArea(s);
			textArea.setLineWrap(true);
			textArea.setWrapStyleWord(true);
			textArea.setMargin(new Insets(5, 5, 5, 5));
			scrollPane.getViewport().setView(textArea);
			message = scrollPane;
			JOptionPane.showMessageDialog(null, message, "Current High Scores",
					JOptionPane.OK_OPTION);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param sTag
	 * @param eElement
	 * @return nValue.getNodeValue()
	 */
	private static String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
				.getChildNodes();

		Node nValue = nlList.item(0);

		return nValue.getNodeValue();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	public ArrayList<String> getArrayList() {
		return this.scoresArray;
	}

}